#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
@Project ：Jmpy
@File ：testJmpyBox.py
@IDE ：PyCharm
@Author ：Link
@Date ：2025/4/12 11:08
"""
import time
import unittest

import pandas as pd

from jslBaseInterface.jmp_box import JmpyBox
from jslBaseInterface.jmp_dataframe import JmpDataFrame, JmpyColType
from jslBaseInterface.jmp_file import JmpyFile
from jslBasePlot.jmp_plot_gen import JmpyPlotGen


class MyTestCase(unittest.TestCase):

    def setUp(self) -> None:
        """

        """
        print("Setup调用")
        self.path = r"TestSwap\TempNoLimit.csv"
        self.limit_csv = r"TestSwap\Temp.csv"
        self.script = []

    def tearDown(self) -> None:
        time.sleep(0.1)
        print("tearDown调用")
        if not self.script:
            return
        JmpyFile.jExecuteScript(
            *self.script
        )

    def testRdPandasData(self):
        df = pd.read_csv(self.path)
        assert isinstance(df, pd.DataFrame)
        assert not df.empty
        print(df)

    def testClear(self):
        self.script = [JmpyFile.jCloseAll()]

    def testRdFileShowTable(self):
        """
        读取csv文件并显示DataTable
        :return:
        """
        self.script = [
            JmpyFile.jLoadCsv(self.path, in_vis=False),
        ]

    def testRdFileHideTable(self):
        """
        读取csv文件并不显示DataTable
        :return:
        """
        self.script = [
            JmpyFile.jLoadCsv(self.path, in_vis=True),
        ]

    def testRdFileSetRowShowTable(self):
        """
        读取一些非第二行是数据的CsvData
        :return:
        """
        self.script = [
            JmpyFile.jLoadCsv(self.limit_csv, in_vis=False, row_start=11),
        ]

    def testRdFileSetRowShowTableSetColTypeAndLimit(self):
        """
        1. 读取一些非第二行是数据的CsvData
        2. 设置X_COORD和Y_COORD为字符串类型
        3. 配置Limit
        :return:
        """
        self.script = [
            JmpyFile.jLoadCsv(self.limit_csv, in_vis=False, row_start=11),
            JmpDataFrame.jSetColType("X_COORD", JmpyColType.String),  # 设置列格式为名义型
            JmpDataFrame.jSetColType("Y_COORD", JmpyColType.String),  # 设置列格式为名义型
            JmpDataFrame.jAddSpecification("OS", -0.8, -0.6),  # 设置OS列的规格限
            JmpDataFrame.jAddSpecification("Leakage", -0.000001, 0.000002),  # 设置OS列的规格限
            JmpDataFrame.jAddSpecification("Ron", 9, 17),  # 设置Ron列的规格限
        ]

    def testPlotScatter(self):
        """
        基本的绘图教程
        绘制散点图
        :return:
        """
        window_name = "myWin"
        self.script = [
            JmpyFile.jLoadCsv(self.limit_csv, in_vis=False, row_start=11),
            JmpDataFrame.jSetColType("X_COORD", JmpyColType.String),  # 设置列格式为名义型
            JmpDataFrame.jSetColType("Y_COORD", JmpyColType.String),  # 设置列格式为名义型
            JmpDataFrame.jSetColType("FAIL_FLAG", JmpyColType.Bin),  # 设置列格式为连续型
            JmpDataFrame.jAddSpecification("OS", -0.8, -0.6),  # 设置OS列的规格限
            JmpDataFrame.jAddSpecification("Leakage", -0.000001, 0.000002),  # 设置OS列的规格限
            JmpDataFrame.jAddSpecification("Ron", 9, 17),  # 设置Ron列的规格限
            JmpyBox.jNewWin(
                JmpyBox.jVListBox(
                    JmpyPlotGen.jGenScatter(col="OS", c_min=-0.8 - 0.01, c_max=-0.6 + 0.01, lo_limit=-0.8, hi_limit=-0.6),
                )
                , title="数据散点图",
                rbiv=window_name
            ),
            # JmpyFile.jCloseAll()
        ]

    def testScript(self):
        window_name = "myWin"

        script = JmpDataFrame.jNewScript(
            "执行数据散点图",
            JmpyBox.jNewWin(
                JmpyBox.jVListBox(
                    JmpyPlotGen.jGenScatter(col="OS", c_min=-0.8 - 0.01, c_max=-0.6 + 0.01, lo_limit=-0.8, hi_limit=-0.6),
                )
                , title="数据散点图",
                rbiv=window_name
            ), )

        # save_pdf_path = r"TestSwap\testMultiPlot.png"
        # save_html_path = r"TestSwap\testMultiPlot.html"

        self.script = [
            JmpyFile.jLoadCsv(self.limit_csv, in_vis=False, row_start=11),
            JmpDataFrame.jSetColType("X_COORD", JmpyColType.String),  # 设置列格式为名义型
            JmpDataFrame.jSetColType("Y_COORD", JmpyColType.String),  # 设置列格式为名义型
            JmpDataFrame.jSetColType("FAIL_FLAG", JmpyColType.Bin),  # 设置列格式为连续型
            JmpDataFrame.jAddSpecification("OS", -0.8, -0.6),  # 设置OS列的规格限
            JmpDataFrame.jAddSpecification("Leakage", -0.000001, 0.000002),  # 设置OS列的规格限
            JmpDataFrame.jAddSpecification("Ron", 9, 17),  # 设置Ron列的规格限
            script,
            # JmpyFile.jSavePng(save_pdf_path, rbiv=window_name),
            # JmpyFile.jSaveInteractiveHtml(save_html_path, rbiv=window_name),
            # JmpyFile.jCloseAll()
            # JmpyFile.jCloseAll()
        ]

    @staticmethod
    def testMultiPlot():
        """
        基本的绘图教程
        绘制散点图
        :return:
        """
        JmpyFile.jExecuteScript(
            JmpyFile.jLoadCsv(r"TestSwap\Temp.csv", in_vis=False, row_start=11),  # 读取测试数据文件

            JmpDataFrame.jSetColType("X_COORD", JmpyColType.String),  # 设置列格式为名义型
            JmpDataFrame.jSetColType("Y_COORD", JmpyColType.String),  # 设置列格式为名义型
            JmpDataFrame.jSetColType("FAIL_FLAG", JmpyColType.Bin),  # 设置列格式为连续型

            JmpDataFrame.jAddSpecification("OS", -0.8, -0.6),  # 设置OS列的规格限
            JmpDataFrame.jAddSpecification("Leakage", -0.000001, 0.000002),  # 设置OS列的规格限
            JmpDataFrame.jAddSpecification("Ron", 9, 17),  # 设置Ron列的规格限

            JmpyBox.jNewWin(
                JmpyBox.jGroupBox(
                    JmpyPlotGen.jGenScatter(col="OS", c_min=-0.8 - 0.01, c_max=-0.6 + 0.01, lo_limit=-0.8, hi_limit=-0.6),  # 散点图
                    JmpyPlotGen.jGenHist(col="OS", c_min=-0.75 - 0.01, c_max=-0.73 + 0.01, lo_limit=-0.8, hi_limit=-0.6),  # 柱状图
                    JmpyPlotGen.jGenScatter(col="Leakage", c_min=-0.000001, c_max=0.000002, lo_limit=-0.000001, hi_limit=0.000002), # 散点图
                    JmpyPlotGen.jGenScatter(col="Ron", c_min=9 - 1, c_max=17 + 1, lo_limit=9, hi_limit=17),# 散点图
                    title="绘图框",
                    col=2  # 绘图框的列数
                ),
                title="多个图形的数据组合窗口",
            ),
        )

    def testJBaseContDis(self):
        print(JmpyPlotGen.jBaseContDis("OS", -0.8, -0.6))


if __name__ == '__main__':
    unittest.main()
